package org.edu.modules.stu.stu.service.impl;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.dto.BaseCountDto;
import org.edu.common.utils.ConvertUtils;
import org.edu.modules.stu.base.entity.VeBaseStudent;
import org.edu.modules.stu.base.service.VeBaseStudentService;
import org.edu.modules.stu.base.service.VeStuFileService;
import org.edu.modules.stu.common.entity.VeBaseBanji;
import org.edu.modules.stu.common.service.VeBaseBanjiService;
import org.edu.modules.stu.stu.mapper.VeStuRewardMapper;
import org.edu.modules.stu.stu.entity.VeStuReward;
import org.edu.modules.stu.stu.dto.VeStuRewardDto;
import org.edu.modules.stu.stu.service.VeStuRewardService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * 实现类
 */
@Service
public class VeStuRewardServiceImpl extends BaseServiceImpl<VeStuReward, VeStuRewardMapper> implements VeStuRewardService {

    @Autowired
    private VeStuFileService fileService;
    @Autowired
    private VeBaseBanjiService veBaseBanjiService;

    @Autowired
    private VeBaseStudentService veBaseStudentService;

    public IPage<VeStuRewardDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper) {
        return mapper.pageDto(page, queryWrapper);
    }

    @Override
    public IPage<VeStuRewardDto> pageDtoByUserId(BasePageDto page, Wrapper queryWrapper, String userId) {
        return mapper.pageDtoByUserId(page, queryWrapper, userId);
    }

    /**
     * 查询实体类
     */
    public VeStuRewardDto findDtoById(String id) {
        VeStuReward entity = super.findById(id);
        VeStuRewardDto dto = ConvertUtils.convertToDto(entity, VeStuRewardDto.class);
        return dto;
    }

    /**
     * 新增或者修改
     */
    public void insertOrUpdate(VeStuRewardDto dto, Boolean isAdd) {
        if (dto.getStuId() != null) {
            VeBaseStudent baseStudent = veBaseStudentService.findById(dto.getStuId() + "");
            dto.setBjId(baseStudent.getBjId());
            if (StringUtils.isBlank(baseStudent.getBjText()) && baseStudent.getBjId() != null) {
                VeBaseBanji veBaseBanji = veBaseBanjiService.findDtoById(baseStudent.getBjId() + "");
                if (veBaseBanji != null) {
                    dto.setBjText(veBaseBanji.getXzbmc());
                }
            } else {
                dto.setBjText(baseStudent.getBjText());
            }
            dto.setFalId(baseStudent.getFalId());
            dto.setSpecId(baseStudent.getSpecId());
            dto.setName(baseStudent.getXm());
            dto.setCode(baseStudent.getXh());
        } else {
            dto.setName(dto.getBjText());
        }
        if (isAdd) {
            super.insert((VeStuReward) dto);
        } else {
            super.update((VeStuReward) dto);
        }

        fileService.updateFiles(dto.getFileIds(), dto.getId() + "", "stuReward");
    }

    @Override
    public List<BaseCountDto> countBy(Wrapper queryWrapper) {
        return mapper.countBy(queryWrapper);
    }

    @Override
    public void importExcel(List<Map<String, Object>> mapList) {
        for (Map map : mapList) {
            VeStuRewardDto dto = new VeStuRewardDto();
            //         dto.setStuId ((String)map.get(0000));
            //         dto.setName ((String)map.get(0000));
            //         dto.setCode ((String)map.get(0000));
            //         dto.setFalId ((String)map.get(0000));
            //         dto.setSpecId ((String)map.get(0000));
            //         dto.setTypeId ((String)map.get(0000));
            //         dto.setBjId ((String)map.get(0000));
            //         dto.setFileId ((String)map.get(0000));
            //         dto.setRewardTime ((String)map.get(0000));
            //         dto.setContent ((String)map.get(0000));

            super.insert(dto);
        }

    }

}
